home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c17 / CodePackager.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  13.6 KB  |  436 lines

  1. //: CodePackager.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // "Packs" and "unpacks" the code in "Thinking 
  50. // in Java" for cross-platform distribution.
  51. /* Commented so CodePackager sees it and starts
  52.    a new chapter directory, but so you don't 
  53.    have to worry about the directory where this
  54.    program lives:
  55. package c17;
  56. */
  57. import java.util.*;
  58. import java.io.*;
  59.  
  60. class Pr {
  61.   static void error(String e) {
  62.     System.err.println("ERROR: " + e);
  63.     System.exit(1);
  64.   }
  65. }
  66.  
  67. class IO {
  68.   static BufferedReader disOpen(File f) {
  69.     BufferedReader in = null;
  70.     try {
  71.       in = new BufferedReader(
  72.         new FileReader(f));
  73.     } catch(IOException e) {
  74.       Pr.error("could not open " + f);
  75.     }
  76.     return in;
  77.   }
  78.   static BufferedReader disOpen(String fname) {
  79.     return disOpen(new File(fname));
  80.   }
  81.   static DataOutputStream dosOpen(File f) {
  82.     DataOutputStream in = null;
  83.     try {
  84.       in = new DataOutputStream(
  85.         new BufferedOutputStream(
  86.           new FileOutputStream(f)));
  87.     } catch(IOException e) {
  88.       Pr.error("could not open " + f);
  89.     }
  90.     return in;
  91.   }
  92.   static DataOutputStream dosOpen(String fname) {
  93.     return dosOpen(new File(fname));
  94.   }
  95.   static PrintWriter psOpen(File f) {
  96.     PrintWriter in = null;
  97.     try {
  98.       in = new PrintWriter(
  99.         new BufferedWriter(
  100.           new FileWriter(f)));
  101.     } catch(IOException e) {
  102.       Pr.error("could not open " + f);
  103.     }
  104.     return in;
  105.   }
  106.   static PrintWriter psOpen(String fname) {
  107.     return psOpen(new File(fname));
  108.   }
  109.   static void close(Writer os) {
  110.     try {
  111.       os.close();
  112.     } catch(IOException e) {
  113.       Pr.error("closing " + os);
  114.     }
  115.   }
  116.   static void close(DataOutputStream os) {
  117.     try {
  118.       os.close();
  119.     } catch(IOException e) {
  120.       Pr.error("closing " + os);
  121.     }
  122.   }
  123.   static void close(Reader os) {
  124.     try {
  125.       os.close();
  126.     } catch(IOException e) {
  127.       Pr.error("closing " + os);
  128.     }
  129.   }
  130. }
  131.  
  132. class SourceCodeFile {
  133.   public static final String 
  134.     startMarker = "//:", // Start of source file
  135.     endMarker = "} ///:~", // End of source
  136.     endMarker2 = "}; ///:~", // C++ file end
  137.     beginContinue = "} ///:Continued",
  138.     endContinue = "///:Continuing",
  139.     packMarker = "###", // Packed file header tag
  140.     eol = // Line separator on current system
  141.       System.getProperty("line.separator"),
  142.     filesep = // System's file path separator
  143.       System.getProperty("file.separator");
  144.   public static String copyright = "";
  145.   static {
  146.     try {
  147.       BufferedReader cr =
  148.         new BufferedReader(
  149.           new FileReader("Copyright.txt"));
  150.       String crin;
  151.       while((crin = cr.readLine()) != null)
  152.         copyright += crin + "\n";
  153.       cr.close();
  154.     } catch(Exception e) {
  155.       copyright = "";
  156.     }
  157.   }
  158.   private String filename, dirname,
  159.     contents = new String();
  160.   private static String chapter = "c02";
  161.   // The file name separator from the old system:
  162.   public static String oldsep;
  163.   public String toString() {
  164.     return dirname + filesep + filename;
  165.   }
  166.   // Constructor for parsing from document file:
  167.   public SourceCodeFile(String firstLine, 
  168.       BufferedReader in) {
  169.     dirname = chapter;
  170.     // Skip past marker:
  171.     filename = firstLine.substring(
  172.         startMarker.length()).trim();
  173.     // Find space that terminates file name:
  174.     if(filename.indexOf(' ') != -1)
  175.       filename = filename.substring(
  176.           0, filename.indexOf(' '));
  177.     System.out.println("found: " + filename);
  178.     contents = firstLine + eol;
  179.     if(copyright.length() != 0)
  180.       contents += copyright + eol;
  181.     String s;
  182.     boolean foundEndMarker = false;
  183.     try {
  184.       while((s = in.readLine()) != null) {
  185.         if(s.startsWith(startMarker))
  186.           Pr.error("No end of file marker for " +
  187.             filename);
  188.         // For this program, no spaces before 
  189.         // the "package" keyword are allowed
  190.         // in the input source code:
  191.         else if(s.startsWith("package")) {
  192.           // Extract package name:
  193.           String pdir = s.substring(
  194.             s.indexOf(' ')).trim();
  195.           pdir = pdir.substring(
  196.             0, pdir.indexOf(';')).trim();
  197.           // Capture the chapter from the package
  198.           // ignoring the 'com' subdirectories:
  199.           if(!pdir.startsWith("com")) {
  200.             int firstDot = pdir.indexOf('.');
  201.             if(firstDot != -1)
  202.               chapter = 
  203.                 pdir.substring(0,firstDot);
  204.             else
  205.               chapter = pdir;
  206.           }
  207.           // Convert package name to path name:
  208.           pdir = pdir.replace(
  209.             '.', filesep.charAt(0));
  210.           System.out.println("package " + pdir);
  211.           dirname = pdir;
  212.         }
  213.         contents += s + eol;
  214.         // Move past continuations:
  215.         if(s.startsWith(beginContinue))
  216.           while((s = in.readLine()) != null)
  217.             if(s.startsWith(endContinue)) {
  218.               contents += s + eol;
  219.               break;
  220.             }
  221.         // Watch for end of code listing:
  222.         if(s.startsWith(endMarker) ||
  223.            s.startsWith(endMarker2)) {
  224.           foundEndMarker = true;
  225.           break;
  226.         }
  227.       }
  228.       if(!foundEndMarker)
  229.         Pr.error(
  230.           "End marker not found before EOF");
  231.       System.out.println("Chapter: " + chapter);
  232.     } catch(IOException e) {
  233.       Pr.error("Error reading line");
  234.     }
  235.   }
  236.   // For recovering from a packed file:
  237.   public SourceCodeFile(BufferedReader pFile) {
  238.     try {
  239.       String s = pFile.readLine();
  240.       if(s == null) return;
  241.       if(!s.startsWith(packMarker))
  242.         Pr.error("Can't find " + packMarker
  243.           + " in " + s);
  244.       s = s.substring(
  245.         packMarker.length()).trim();
  246.       dirname = s.substring(0, s.indexOf('#'));
  247.       filename = s.substring(s.indexOf('#') + 1);
  248.       dirname = dirname.replace(
  249.         oldsep.charAt(0), filesep.charAt(0));
  250.       filename = filename.replace(
  251.         oldsep.charAt(0), filesep.charAt(0));
  252.       System.out.println("listing: " + dirname 
  253.         + filesep + filename);
  254.       while((s = pFile.readLine()) != null) {
  255.         // Watch for end of code listing:
  256.         if(s.startsWith(endMarker) ||
  257.            s.startsWith(endMarker2)) {
  258.           contents += s;
  259.           break;
  260.         }
  261.         contents += s + eol;
  262.       }
  263.     } catch(IOException e) {
  264.       System.err.println("Error reading line");
  265.     }
  266.   }
  267.   public boolean hasFile() { 
  268.     return filename != null; 
  269.   }
  270.   public String directory() { return dirname; }
  271.   public String filename() { return filename; }
  272.   public String contents() { return contents; }
  273.   // To write to a packed file:
  274.   public void writePacked(DataOutputStream out) {
  275.     try {
  276.       out.writeBytes(
  277.         packMarker + dirname + "#" 
  278.         + filename + eol);
  279.       out.writeBytes(contents);
  280.     } catch(IOException e) {
  281.       Pr.error("writing " + dirname + 
  282.         filesep + filename);
  283.     }
  284.   }
  285.   // To generate the actual file:
  286.   public void writeFile(String rootpath) {
  287.     File path = new File(rootpath, dirname);
  288.     path.mkdirs();
  289.     PrintWriter p =
  290.       IO.psOpen(new File(path, filename));
  291.     p.print(contents);
  292.     IO.close(p);
  293.   }
  294. }
  295.  
  296. class DirMap {
  297.   private Hashtable t = new Hashtable();
  298.   private String rootpath;
  299.   DirMap() {
  300.     rootpath = System.getProperty("user.dir");
  301.   }
  302.   DirMap(String alternateDir) {
  303.     rootpath = alternateDir;
  304.   }
  305.   public void add(SourceCodeFile f){
  306.     String path = f.directory();
  307.     if(!t.containsKey(path))
  308.       t.put(path, new Vector());
  309.     ((Vector)t.get(path)).addElement(f);
  310.   }
  311.   public void writePackedFile(String fname) {
  312.     DataOutputStream packed = IO.dosOpen(fname);
  313.     try {
  314.       packed.writeBytes("###Old Separator:" +
  315.         SourceCodeFile.filesep + "###\n");
  316.     } catch(IOException e) {
  317.       Pr.error("Writing separator to " + fname);
  318.     }
  319.     Enumeration e = t.keys();
  320.     while(e.hasMoreElements()) {
  321.       String dir = (String)e.nextElement();
  322.       System.out.println(
  323.         "Writing directory " + dir);
  324.       Vector v = (Vector)t.get(dir);
  325.       for(int i = 0; i < v.size(); i++) {
  326.         SourceCodeFile f = 
  327.           (SourceCodeFile)v.elementAt(i);
  328.         f.writePacked(packed);
  329.       }
  330.     }
  331.     IO.close(packed);
  332.   }
  333.   // Write all the files in their directories:
  334.   public void write() {
  335.     Enumeration e = t.keys();
  336.     while(e.hasMoreElements()) {
  337.       String dir = (String)e.nextElement();
  338.       Vector v = (Vector)t.get(dir);
  339.       for(int i = 0; i < v.size(); i++) {
  340.         SourceCodeFile f = 
  341.           (SourceCodeFile)v.elementAt(i);
  342.         f.writeFile(rootpath);
  343.       }
  344.       // Add file indicating file quantity
  345.       // written to this directory as a check:
  346.       IO.close(IO.dosOpen(
  347.         new File(new File(rootpath, dir),
  348.           Integer.toString(v.size())+".files")));
  349.     }
  350.   }
  351. }
  352.  
  353. public class CodePackager {
  354.   private static final String usageString =
  355.   "usage: java CodePackager packedFileName" +
  356.   "\nExtracts source code files from packed \n" +
  357.   "version of Tjava.doc sources into " +
  358.   "directories off current directory\n" +
  359.   "java CodePackager packedFileName newDir\n" +
  360.   "Extracts into directories off newDir\n" +
  361.   "java CodePackager -p source.txt packedFile" +
  362.   "\nCreates packed version of source files" +
  363.   "\nfrom text version of Tjava.doc";
  364.   private static void usage() {
  365.     System.err.println(usageString);
  366.     System.exit(1);
  367.   }
  368.   public static void main(String[] args) {
  369.     if(args.length == 0) usage();
  370.     if(args[0].equals("-p")) {
  371.       if(args.length != 3)
  372.         usage();
  373.       createPackedFile(args);
  374.     }
  375.     else {
  376.       if(args.length > 2)
  377.         usage();
  378.       extractPackedFile(args);
  379.     }
  380.   }
  381.   private static String currentLine; 
  382.   private static BufferedReader in;
  383.   private static DirMap dm;
  384.   private static void 
  385.   createPackedFile(String[] args) {
  386.     dm = new DirMap();
  387.     in = IO.disOpen(args[1]);
  388.     try {
  389.       while((currentLine = in.readLine()) 
  390.           != null) {
  391.         if(currentLine.startsWith(
  392.             SourceCodeFile.startMarker)) {
  393.           dm.add(new SourceCodeFile(
  394.                    currentLine, in));
  395.         }
  396.         else if(currentLine.startsWith(
  397.             SourceCodeFile.endMarker))
  398.           Pr.error("file has no start marker");
  399.         // Else ignore the input line
  400.       }
  401.     } catch(IOException e) {
  402.       Pr.error("Error reading " + args[1]);
  403.     }
  404.     IO.close(in);
  405.     dm.writePackedFile(args[2]);
  406.   }
  407.   private static void 
  408.   extractPackedFile(String[] args) {
  409.     if(args.length == 2) // Alternate directory
  410.       dm = new DirMap(args[1]);
  411.     else // Current directory
  412.       dm = new DirMap();
  413.     in = IO.disOpen(args[0]);
  414.     String s = null;
  415.     try {
  416.        s = in.readLine();
  417.     } catch(IOException e) {
  418.       Pr.error("Cannot read from " + in);
  419.     }
  420.     // Capture the separator used in the system
  421.     // that packed the file:
  422.     if(s.indexOf("###Old Separator:") != -1 ) {
  423.       String oldsep = s.substring(
  424.         "###Old Separator:".length());
  425.       oldsep = oldsep.substring(
  426.         0, oldsep. indexOf('#'));
  427.       SourceCodeFile.oldsep = oldsep;
  428.     }
  429.     SourceCodeFile sf = new SourceCodeFile(in);
  430.     while(sf.hasFile()) {
  431.       dm.add(sf);
  432.       sf = new SourceCodeFile(in);
  433.     }
  434.     dm.write();
  435.   }
  436. } ///:~